package de.breitbeil.jbooking.ui.view;

import de.breitbeil.jbooking.domain.Booking;
import de.breitbeil.jbooking.ui.dataview.DataView;
import de.breitbeil.jbooking.ui.dataview.DataViewTable;
import de.breitbeil.jbooking.ui.dataview.columns.DataViewColumn;
import de.breitbeil.jbooking.ui.dataview.columns.DataViewColumnImpl;
import de.breitbeil.jbooking.ui.dataview.columns.I18NDataViewColumn;
import de.breitbeil.jbooking.ui.dataview.columns.PropertyDataViewColumn;
import de.breitbeil.jbooking.ui.form.BookingForm;
import de.breitbeil.jbooking.ui.view.tablecellrenderer.ConcatenateTableCellRenderer;
import de.breitbeil.jbooking.ui.view.tablecellrenderer.PersonTableCellRenderer;

public class BookingView extends DataView {

    public BookingView() throws InstantiationException, IllegalAccessException {
        super("bookingView", Booking.class, BookingForm.class);
    }
    
    @Override
    public void init() {
        addColumn(new I18NDataViewColumn("bookingType.text"));
        addColumn(new PropertyDataViewColumn("from"));
        addColumn(new PropertyDataViewColumn("to", false, true));
        addColumn(new PropertyDataViewColumn("numDays"));
        addColumn(new PropertyDataViewColumn("numberAdults"));
        addColumn(new PropertyDataViewColumn("numberChildren"));
        addColumn(new PropertyDataViewColumn("numberDogs"));
        //  addColumn(new PropertyDataViewColumn("prepayment"));
        addColumn(new PropertyDataViewColumn("vtax"));
        addColumn(new PropertyDataViewColumn("price"));
        addColumn(createPersonColumn());
        
        addI18NTextFilter("bookingType.text");
    }

    protected DataViewColumnImpl createPersonColumn() {
        PersonTableCellRenderer renderer = new PersonTableCellRenderer();
        DataViewColumnImpl column = new DataViewColumnImpl("person");
        column.setFilterProperties(new String[]{"person.lastname", "person.firstname"});
        column.setCellRenderer(renderer);
        column.setComparator(renderer);
        return column;
    }
    
	@Override
	protected DataViewTable createDataViewTable() {
		return new DataViewTable(getId(), getColumns()) {
			@Override
			protected void updateStatusBar() {
				int numDays = getNumDays();
				double sum = getPriceSum();
				double sum_vtax = getVtaxSum();
				
				String message = getMessage(getId() + ".bookingSums.message", new Object[]{numDays, sum_vtax, sum});
				getStatusBar().setMessage(message);
			}
			
			protected int getNumDays() {
				int numDays = 0;
				for(Object o: getFinalEventList()) {
					if(o instanceof Booking) {
						Booking booking = (Booking) o;
						numDays = numDays + booking.getNumDays();
					}
				}
				return numDays;
			}
			
			protected double getPriceSum() {
				double sum = 0;
				for(Object o: getFinalEventList()) {
					if(o instanceof Booking) {
						Booking booking = (Booking) o;
						if(booking.getPrice() != null)
							sum = sum + booking.getPrice();
						
						// if(booking.getPrepayment() != null)
						//	sum = sum + booking.getPrepayment();
					}
				}
				return sum;
			}
			protected double getVtaxSum() {
				double sum_vtax = 0;
				for(Object o: getFinalEventList()) {
					if(o instanceof Booking) {
						Booking booking = (Booking) o;
						if(booking.getVtax() != null)
							sum_vtax = sum_vtax + booking.getVtax();
					}
				}
				return sum_vtax;
			}
		};
	}
	


}
